package middleware

import (
	"log"

	"github.com/gin-gonic/gin"
)

func IPAllowsMiddleware(ips []string) gin.HandlerFunc {

	return func(ctx *gin.Context) {
		log.Println(ctx.ClientIP())
		allow := false
		for _, ip := range ips {
			if ctx.ClientIP() == ip {
				allow = true
				break
			}
		}
		if !allow {
			log.Fatal("ip is invalid")
			ctx.Abort()
			return
		}
		ctx.Next()
	}
}
